"use strict";
import Vue from "vue";
import Vuex from "vuex";
import cookie from "js-cookie";
import { getMenusByUserId, login,tokenRefresh } from "@/api/system.js";

import { selectAll } from "@/api/dataDictionary.js";
import { selectAllUser } from "@/api/user.js";
import { selectSystemInfoAll } from "@/api/system.js";
import { myTaskCount } from "@/api/process.js";
import { getPermissionListByUserName } from "@/api/permission.js";
import { getOrgAllList } from "@/api/org.js";




Vue.use(Vuex);
const store = new Vuex.Store({
  state: {
	  tabLock:false,
    //这里放全局参数
    token: "" || cookie.get("token"),
	 menu: "" || cookie.get("menu"),
	dataDictionary:[],
	userList:[],
	user:cookie.get("user"),
	activeUserName:"",
	userChineseName:"",
	systemList:[],
	permissionList:[],
	orgList:[],
	taskNum:0,
	subTitle:'',
	isCollapse:false,
	panes:[
	  { title: "首 页", key: "/main/", closable: false, icon: "cloud",query:{} }
	]
	
  },
  getters: {
	  getIsCollapse:state => state.isCollapse,
	  getTabLock:state => state.tabLock,
	  getPanes:state => state.panes,
	getSubtitle:state => state.subTitle,
    getToken: state => state.token,
	  getMenu: state => state.menu,
	  dict:(state,getters)=>(dictCode)=>{
		  return  state.dataDictionary.filter(item=>{
			  return item.dictCode == dictCode;
		  });
	  },
	  getDataDictionaryItem:(state,getters)=>(dictCode,itemCode)=>{
	  		   let dataDictionary =   state.dataDictionary.filter(item=>{
	  			  return (item.dictCode == dictCode) && (item.itemCode == itemCode);
	  		  })
			  if(dataDictionary.length!=0){
				   return dataDictionary[0];
			  }else{
				   return null;
			  }
			 
	  },
	  
	  vaildPermission:(state,getters)=>(permissionId)=>{
	  		let permission =   state.permissionList.filter(item=>{
				return (item.permissionId == permissionId);
	  		  })
			return permission.length>0;
	  },
	  
	  getUser:(state,getters)=>(userName)=>{
	  		   let user =   state.userList.filter(item=>{
	  			  return (item.userName == userName);
	  		  })
			  if(user.length>0){
				   return user[0];
			  }else{
				  return {};
			  }
			  
			 
	  },
	  getUserList:(state,getters)=>()=>{
	  		return  state.userList;
	  },
	  getOrgList:(state,getters)=>()=>{
	  		return  state.orgList;
	  },
	  	  getActiveUser:(state,getters)=>()=>{
			  if(state.user!="" & state.user != undefined){
				   return JSON.parse(state.user)
			  }else {
				   return null;
			  }
			
	  }, 
	  
	  
	  getSystemList:(state,getters)=>()=>{
	  		return  state.systemList;
	  },
	  
	 getSystem:(state,getters)=>(systemNo)=>{
	 		   let system =   state.systemList.filter(item=>{
	 			  return (item.systemNo == systemNo)
	 		  })
	 			  if(system.length!=0){
	 				   return system[0];
	 			  }else{
	 				   return null;
	 			  }
	 },
	 
	 getOrg:(state,getters)=>(seqCate)=>{
	 		   let org =   state.orgList.filter(item=>{
	 			  return (item.seqCate == seqCate)
	 		  })
	 			  if(org.length!=0){
	 				   return org[0];
	 			  }else{
	 				   return null;
	 			  }
	 },
	 
	 getTaskNum: state => state.taskNum
	 
/*  getActiveUserName:(state,getters)=>()=>{
	  		if(state.user==''){
				return cookie.get("userChineseName")
			}else{
				return state.activeUserName
			}
	  } */
	  
  },
  mutations: {
	  SET_ISCOLLAPSE: (state, isCollapse) => {
	  	
	    state.isCollapse = isCollapse;
	  },
	  SET_TABLOCK: (state, tabLock) => {
	  	
	    state.tabLock = tabLock;
	  },
	  
	  SET_PANELS: (state, panes) => {
		
      state.panes = panes;
    },
    //这里是set方法
	SET_SUBTITLE: (state, subTitle) => {
		
      state.subTitle = subTitle;
    },
    INIT_TOKEN: state => {
      state.token = "" || cookie.get("token");
    },

    CLEAR_TOKEN: state => {
      state.token = "";
      cookie.set("token", "");
    },
    SET_TOKEN: (state, token) => {
      state.token = token;
      cookie.set("token", token);
    },
	SET_MENU: (state, menu) => {
	  state.menu = JSON.stringify(menu);
	  cookie.set("menu", JSON.stringify(menu));
	},
	SET_DICT: (state, dict) => {
	  state.dataDictionary = dict;
	},
	SET_USER_LIST: (state, userList) => {
	  state.userList = userList;
	},
	SET_PERMISSION_LIST: (state, permissionList) => {
	  state.permissionList = permissionList;
	},
	SET_USER: (state, user) => {
	  state.user =  JSON.stringify(user);
	   cookie.set("user", JSON.stringify(user));
	   /* state.userChineseName = user.userChineseName;
	  state.userName = user.userName;
	    cookie.set("userChineseName", user.userChineseName);
		 cookie.set("userName", user.userName); */
	},
	
	CLEAR_USER: (state) => {
	   state.user =  "";
	   cookie.set("user", "");
	   /* state.userChineseName = user.userChineseName;
	  state.userName = user.userName;
	    cookie.set("userChineseName", user.userChineseName);
		 cookie.set("userName", user.userName); */
	},
	
	
	SET_SYSTEM_LIST: (state, systemList) => {
	  state.systemList = systemList;
	},
	
	SET_ORG_LIST: (state, orgList) => {
	  state.orgList = orgList;
	},
	
	SET_TASKMUN: (state, taskNum) => {
	  state.taskNum = taskNum;
	},
  },
  actions: {
	  //获取用户菜单
	  setTabLock({
			commit,state
	    },tabLock) {
			commit('SET_TABLOCK', tabLock);
	    },
	  
	  //获取用户菜单
	  setPanels({
	    	commit,state
	    },panels) {
			commit('SET_PANELS', panels);
	  			
	    },
	  removePanel({
	    	commit,state
	    },panel) {
			 return new Promise((resolve, reject) => {
				 let panes =  state.panes
				 let newPanes = panes.filter(tab=>tab.key!==panel.key);
				 commit('SET_PANELS', newPanes);
				 resolve();
			 });
	  			
	    },
	  
	  
	  //获取用户菜单
	initDict({
	  	commit
	  }) {
		  return new Promise((resolve, reject) => {
			  selectAll().then(response=>{
			  	let dict =response.data.extend;
				commit('SET_DICT', dict);
				resolve();
			  }).catch(error=>{ 
				  reject(error)
			  });
			});
	  },
	  
	  //获取系统用户信息
	  initUserList({
	    	commit
	    }) {
	  	  return new Promise((resolve, reject) => {
	  		  selectAllUser().then(response=>{
	  		  	let userList = response.data.extend;
	  			commit('SET_USER_LIST', userList);
	  			resolve();
	  		  }).catch(error=>{ 
	  			  reject(error)
	  		  });
	  		});
	    },
		
		
		//获取系统信息
		initSystemList({
		  	commit
		  }) {
			  return new Promise((resolve, reject) => {
				  selectSystemInfoAll().then(response=>{
				  	let systemList = response.data.extend;
					commit('SET_SYSTEM_LIST', systemList);
					resolve();
				  }).catch(error=>{ 
					  reject(error)
				  });
				});
		  },
		  
		  
		  initOrgList({
		  	commit
		  }) {
			  return new Promise((resolve, reject) => {
				  getOrgAllList().then(response=>{
				  	let orgList = response.data.extend;
					commit('SET_ORG_LIST', orgList);
					resolve();
				  }).catch(error=>{ 
					  reject(error)
				  });
				});
		  },
		  
		  updateTaskNum({commit,state}){
			 return new Promise((resolve, reject) => {
			  myTaskCount().then(response=>{
			    	let count = response.data.extend;
			  	commit('SET_TASKMUN', count);
			   resolve();
			    }).catch(error=>{ 
			  	  reject(error.message);
			    });	 
				  });
		  },
		  updateIsCollapse({commit,state},isCollapse){
				commit('SET_ISCOLLAPSE', isCollapse);
		  },
		getPermissionListByUserName({ commit }){
			return new Promise((resolve, reject) => {
			  getPermissionListByUserName().then(response => {
			    if (response.data.code == "0000") {
			      commit("SET_PERMISSION_LIST", response.data.extend);
			      resolve();
			    } else {
			      reject(response.data.message);
			    }
			  }).catch((error)=>{
						 reject(error.message);
					});
			});
			//permissionList
		},
    Login({ commit }, data) {
      return new Promise((resolve, reject) => {
        login(data).then(response => {
          if (response.data.code == "0000") {
            commit("SET_USER", response.data.extend);
            resolve();
          } else {
            reject(response.data.message);
          }
        }).catch((error)=>{
			 reject(error.message);
		});
      });
    },
	
	tokenRefresh({commit},data){
		return new Promise((resolve, reject) => {
		  tokenRefresh(data).then(response => {
		    if (response.data.code == "0000") {
		      commit("SET_USER", response.data.extend);
		      resolve();
		    } else {
		      reject(response.data.message);
		    }
		  }).catch((error)=>{
					 reject(error.message);
		  });
		});
	},
	
	
	
	
    LogOut({ commit }) {
      commit("CLEAR_USER");
    },
    //获取用户菜单
    GetMenusByUserId({ commit }) {
      return new Promise((resolve, reject) => {
        getMenusByUserId()
          .then(response => {
            let menu = response.data.extend;
            commit("SET_MENU", menu);
            resolve();
          })
          .catch(error => {
            reject();
          });
      });
    }
  }
});

export default store;
